/**********************************************************
 * DateUtil.java
 * Created by ankur
 * Last modified Date: Aug 5, 2008 11:41:03 AM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.util;

import org.apache.log4j.Logger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.hibernate.HibernateException;
import org.joda.time.DateTime;
import static com.printeverywhere.util.AdminConstants.*;

/**
 * The Class DateUtil. The class contains utility methods for date operations
 */
public class DateUtil {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(DateUtil.class);

	/** The date format. */
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"dd/MM/yyyy");

	/**
	 * Gets the first date.
	 *
	 * @return the first date
	 */
	public static Date getFirstDate() {
		if (logger.isDebugEnabled()) {
			logger.debug("getFirstDate() - start"); //$NON-NLS-1$
		}

		Date returnDate = new Date(0);
		if (logger.isDebugEnabled()) {
			logger.debug("getFirstDate() - end"); //$NON-NLS-1$
		}
		return returnDate;
	}

	/**
	 * Gets the todays date.
	 *
	 * @return the todays date
	 */
	public static Date getTodaysDate() {
		if (logger.isDebugEnabled()) {
			logger.debug("getTodaysDate() - start"); //$NON-NLS-1$
		}

		try {
			Date date = new Date();
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
			Date returnDate = dateFormat.parse(formatter.format(date));
			if (logger.isDebugEnabled()) {
				logger.debug("getTodaysDate() - end"); //$NON-NLS-1$
			}
			return returnDate;
		} catch (Exception e) {
			logger.error("getTodaysDate()", e); //$NON-NLS-1$

			e.printStackTrace();
			Date returnDate = new Date();
			if (logger.isDebugEnabled()) {
				logger.debug("getTodaysDate() - end"); //$NON-NLS-1$
			}
			return returnDate;
		}
	}

	/**
	 * Gets the privious date from current.
	 *
	 * @param days
	 *            the days
	 *
	 * @return the privious date from current
	 */
	public static Date getPriviousDateFromCurrent(int days) {
		if (logger.isDebugEnabled()) {
			logger.debug("getPriviousDateFromCurrent(int) - start"); //$NON-NLS-1$
		}

		DateTime dateTime = new DateTime();
		dateTime.minusDays(days);
		Date returnDate = dateTime.minusDays(days).toDate();
		if (logger.isDebugEnabled()) {
			logger.debug("getPriviousDateFromCurrent(int) - end"); //$NON-NLS-1$
		}
		return returnDate;
	}

	/**
	 * Reset dates time.
	 *
	 * @param date
	 *            the date
	 *
	 * @return the date
	 *
	 * @throws Exception
	 *             the exception
	 */
	public static Date resetDatesTime(Date date) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("resetDatesTime(Date) - start"); //$NON-NLS-1$
		}

		Date returnDate = getDate(getDate(date));
		if (logger.isDebugEnabled()) {
			logger.debug("resetDatesTime(Date) - end"); //$NON-NLS-1$
		}
		return returnDate;
	}

	/**
	 * Gets the date.
	 *
	 * @param d
	 *            the d
	 *
	 * @return the date
	 *
	 * @throws Exception
	 *             the exception
	 */
	public static String getDate(Date d) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getDate(Date) - start"); //$NON-NLS-1$
		}

		try {
			String returnString = dateFormat.format(d);
			if (logger.isDebugEnabled()) {
				logger.debug("getDate(Date) - end"); //$NON-NLS-1$
			}
			return returnString;
		} catch (Exception e) {
			throw e;
		}
	}

	public static String[] getDateFormats() {
		String[] dateFormats = new String[TOTAL_DATES_LENGTH];
		dateFormats[0] = MM_SLASH_DD_SLASH_YYYY;
		dateFormats[1] = DD_SLASH_MM_SLASH_YYYY;
		dateFormats[2] = YYYY_SLASH_MM_SLASH_DD;
		dateFormats[3] = YYYY_SLASH_DD_SLASH_MM;
		dateFormats[4] = MM_SLASH_DD_SLASH_YYYY_HH_COLON_MI_A;
		dateFormats[5] = DD_SLASH_MM_SLASH_YYYY_HH_COLON_MI_A;
		dateFormats[6] = YYYY_SLASH_MM_SLASH_DD_HH_COLON_MI_A;
		dateFormats[7] = YYYY_SLASH_DD_SLASH_MM_HH_COLON_MI_A;
		dateFormats[8] = MMMM_DD_COMMA_YYYY;
		dateFormats[9] = EEEE_MMMM_DD_COMMA_YYYY;
		dateFormats[10] = EEEE_DD_COMMA_MMMM_YYYY_HH_COLON_MI_A;
		dateFormats[11] = EEEE_COMMA_MMMM_DD_COMMA_YYYY_HH_COLON_MI_A;
		dateFormats[12] = MMMM_DD_COMMA_YYYY_HH_COLON_MI_A;
		dateFormats[13] = MMMM_DD_COMMA_YYYY_HH_COLON_MI_COLON_SS_A;
		dateFormats[14] = DD_DASH_MMMM_DASH_YYYY_HH_COLON_MI_COLON_SS_A;
		dateFormats[15] = EEEE_COMMA_MMMM_DD_COMMA_YYYY;
		dateFormats[16] = EEEE_COMMA_MM_MMMM_COMMA_YYYY_HH_COLON_MI_A;
		dateFormats[17] = EEEE_COMMA_MM_MMMM_COMMA_YYYY__DASH__HH_COLON_MI_A;
		dateFormats[18] = MM_SLASH_DD_SLASH_YYYY_HH_COLON_MI;
		dateFormats[19] = YYYY_SLASH_DD_SLASH_MM_HH_COLON_MI;
		dateFormats[20] = YYYY_SLASH_MM_SLASH_DD_HH_COLON_MI;
		dateFormats[21] = EEEE_DD_COMMA_MMMM_YYYY_HH_COLON_MI;
		dateFormats[22] = EEEE_COMMA_MMMM_DD_COMMA_YYYY_HH_COLON_MI;
		dateFormats[23] = MMMM_MM_COMMA_YYYY_HH_COLON_MI_COLON_SS;
		dateFormats[24] = MMMM_DASH_MM_DASH_YYYY_HH_COLON_MI_COLON_SS;
		dateFormats[25] = EEEE_COMMA_MM_MMMM_COMMA_YYYY_HH_COLON_MI;
		dateFormats[26] = EEEE_COMMA_MM_MMMM_COMMA_YYYY_DASH_HH_COLON_MI;
		dateFormats[27] = EEEE_DD_MM_YYYY_HH_COLON_MM;

		return dateFormats;
	}

	/**
	 * Gets the date.
	 *
	 * @param dStr
	 *            the d str
	 *
	 * @return the date
	 *
	 * @throws Exception
	 *             the exception
	 */
	public static Date getDate(String dStr) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getDate(String) - start"); //$NON-NLS-1$
		}

		Date date = null;
		try {
			date = dateFormat.parse(dStr);
		} catch (Exception e) {
			throw e;
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getDate(String) - end"); //$NON-NLS-1$
		}
		return date;
	}

	/**
	 * Returns list of todays date in all the format as per given in
	 * printeverywhere's generalOptions.
	 *
	 * @return dates list
	 */
	public static List<String> getFormattedDates() {
		List<String> dates = new ArrayList<String>();
		try{
		Date date = DateUtil.getTodaysDate();
		String[] datesFormat = DateUtil.getDateFormats();
		for (int index = 0; index < datesFormat.length; index++) {
			SimpleDateFormat dateFormatter = new SimpleDateFormat(
					datesFormat[index]);
			dates.add(index + AdminConstants.BLANK + AdminConstants.DASH + AdminConstants.BLANK
					+ dateFormatter.format(date));
		}}
		catch (Exception e) {
			logger.info("Error in getting formatted dates list ".intern()+e);
			return null;
		}
		return dates;
	}

	public static void main(String[] args) {
		Date date = getTodaysDate();
		String[] dates = getDateFormats();
		for (int index = 0; index < dates.length; index++) {
			SimpleDateFormat sdf = new SimpleDateFormat(dates[index]);
			System.out.println(index + " - " + sdf.format(date));
		}
	}
}